import {defineStore} from 'pinia'
import { ref } from 'vue'
import { userInfoLoginApi } from '@/apis/userAPI'

export const useUserStore = defineStore('user',()=>{
    const  userInfo = ref({})
    const loginerror = ref(0)
    //2.获取接口数据的action
    const getUserInfo = async ({account,password}) => {
         //调用接口获取用户信息
            const res = await userInfoLoginApi({account:account,password:password});
            if(res.code==0){
               userInfo.value = res.data
               loginerror.value = 0
            }else{

               loginerror.value = 1
            }
    }

    //2.1定义清除用户登录信息
    const clearUserInfo = () => {
       userInfo.value = {}
    }

    //3.以对象的方式把state和action返回
    return{
       userInfo,
       loginerror,
       getUserInfo,
       clearUserInfo
    }
   }
  ,{
       persist:true
   })
